/*
* $Id: DeleteOperation.cpp 338 2011-10-02 18:24:43Z volok-aleksej@yandex.ru $
*
* Copyright 2009-2011 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "DeleteOperation.h"
#include "Database/Tables/TableNames.h"
#include "Database/Tables/EvacuationTable.h"
#include "Database/Tables/FamiliesTable.h"
#include "Database/Tables/FieldValuesTable.h"
#include "Database/Tables/JournalTable.h"
#include "Database/Tables/PointsTable.h"
#include "Database/Tables/InspectTable.h"
#include "Database/Tables/HousingsTable.h"
#include "Database/Tables/QueenBeeTable.h"
#include "Database/SqlBinder.h"

CDeleteOperation::CDeleteOperation(){}
CDeleteOperation::~CDeleteOperation(){}
void CDeleteOperation::DeleteValue(IDatabase* pDatabase){
	if(m_content.s_type == content::FAMILY){
		DeleteFamily(pDatabase);
	}else if(m_content.s_type == content::POINT){
		DeletePoint(pDatabase);
	}else if(m_content.s_type == content::JOURNAL){
		DeleteJournal(pDatabase);
	}else if(m_content.s_type == content::QUEEN_BEE){
		DeleteQueenBee(pDatabase);
	}else if(m_content.s_type == content::HOUSING){
		DeleteHousing(pDatabase);
	}
	m_content.Clear();
}

CDeleteOperation::content::content(){
	Clear();
}
void CDeleteOperation::content::Clear(){
	s_type = DEFAULT;
	s_nId = 0;
}

void CDeleteOperation::DeletePoint(IDatabase* pDatabase){
//get id_record of field_values table
	CPointsTable points;
	points.m_content.s_nId = m_content.s_nId;
	IResult* pResult = points.GetValue(pDatabase);
//delete record of field_values table
	if(pResult->GetSizeResult()){
		CFieldValuesTable fieldValues;
		fieldValues.m_content.s_nId = ((CPointsTable::content*)(*pResult)[0])->s_nIdRecord;
		fieldValues.DeleteValue(pDatabase);
	}
	pResult->Free();
//delete point
	points.m_content.s_nId = m_content.s_nId;
	points.DeleteValue(pDatabase);
}

void CDeleteOperation::DeleteFamily(IDatabase* pDatabase){
//delete evacuation of family
	CEvacuationTable evacuation;
	evacuation.m_content.s_nIdFamily = m_content.s_nId;
	evacuation.DeleteValue(pDatabase);
//get id_record of field_values table
	CFamiliesTable family;
	family.m_content.s_nId = m_content.s_nId;
	IResult* pResult = family.GetValue(pDatabase);
//delete record of field_values table
	if(pResult->GetSizeResult() > 0)
	{
		CFieldValuesTable fieldValue;
		fieldValue.m_content.s_nId = ((CFamiliesTable::content*)(*pResult)[0])->s_nIdRecord;
		fieldValue.DeleteValue(pDatabase);
		pResult->Free();
	}
//delete family
	family.m_content.s_nId = m_content.s_nId;
	family.DeleteValue(pDatabase);
}
void CDeleteOperation::DeleteJournal(IDatabase* pDatabase){
//delete all inspect of housing of current inspect;
	CInspectTable inspect;
	inspect.m_content.s_nIdJournal = m_content.s_nId;
	inspect.DeleteValue(pDatabase);
//get id_record of field_values table
	CJournalTable journal;
	journal.m_content.s_nId = m_content.s_nId;
	IResult* pResult = journal.GetValue(pDatabase);
//delete record of field_values table
	if(pResult->GetSizeResult() > 0)
	{
		CFieldValuesTable fieldValue;
		fieldValue.m_content.s_nId = ((CJournalTable::content*)(*pResult)[0])->s_nIdRecord;
		fieldValue.DeleteValue(pDatabase);
		pResult->Free();
	}
//delete current inspect
	journal.m_content.s_nId = m_content.s_nId;
	journal.DeleteValue(pDatabase);
}

void CDeleteOperation::DeleteQueenBee(IDatabase* pDatabase){
	CQueenBeeTable queenBee;
	queenBee.m_content.s_nId = m_content.s_nId;
	queenBee.DeleteValue(pDatabase);
}

void CDeleteOperation::DeleteHousing(IDatabase* pDatabase){
	CHousingsTable housings;
	housings.m_content.s_nId = m_content.s_nId;
	housings.DeleteValue(pDatabase);
}
